package com.walker;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class DatePathGenerator {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

    public List<String> generatePaths(Map<Integer, String> map) {
        Map<Integer, List<String>> dateLists = new HashMap<>();
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            int key = entry.getKey();
            String value = entry.getValue();
            List<String> dates = getPossibleDates(value);
            dateLists.put(key, dates);
        }

        List<String> result = new ArrayList<>();
        List<Integer> keys = new ArrayList<>(map.keySet());
        Collections.sort(keys);
        generateCombinations(dateLists, keys, 0, new ArrayList<String>(), result);
        return result;
    }

    private List<String> getPossibleDates(String value) {
        if (value.contains("+")) {
            int plusIndex = value.indexOf('+');
            String baseDateStr = value.substring(0, plusIndex);
            String nStr = value.substring(plusIndex + 1);
            int n = Integer.parseInt(nStr);
            List<String> dates = new ArrayList<>();
            for (int k = 0; k <= n; k++) {
                LocalDate baseDate = LocalDate.parse(baseDateStr, formatter);
                LocalDate newDate = baseDate.plusDays(k);
                String newDateStr = newDate.format(formatter);
                dates.add(newDateStr);
            }
            return dates;
        } else if (value.contains("-")) {
            int minusIndex = value.indexOf('-');
            String baseDateStr = value.substring(0, minusIndex);
            String nStr = value.substring(minusIndex + 1);
            int n = Integer.parseInt(nStr);
            List<String> dates = new ArrayList<>();
            for (int k = 0; k <= n; k++) {
                LocalDate baseDate = LocalDate.parse(baseDateStr, formatter);
                LocalDate newDate = baseDate.minusDays(k);
                String newDateStr = newDate.format(formatter);
                dates.add(newDateStr);
            }
            return dates;
        } else {
            return Collections.singletonList(value);
        }
    }

    private void generateCombinations(Map<Integer, List<String>> dateLists, List<Integer> keys, int index,
                                      List<String> current, List<String> result) {
        if (index == keys.size()) {
            StringBuilder sb = new StringBuilder("\\");
            for (String date : current) {
                sb.append(date).append('\\');
            }
            String path = sb.toString();
            if (path.endsWith("\\")) {
                path = path.substring(0, path.length() - 1);
            }
            result.add(path);
            return;
        }

        int key = keys.get(index);
        List<String> dates = dateLists.get(key);
        for (String date : dates) {
            current.add(date);
            generateCombinations(dateLists, keys, index + 1, current, result);
            current.remove(current.size() - 1);
        }
    }

    // main 方法用于测试
    public static void main(String[] args) {
        DatePathGenerator generator = new DatePathGenerator();

        // 测试用例 1: 单个日期
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(0, "20220922");
        System.out.println("Test 1: " + generator.generatePaths(map1));

        // 测试用例 2: 两个日期
        Map<Integer, String> map2 = new HashMap<>();
        map2.put(0, "20220922");
        map2.put(1, "20220111");
        System.out.println("Test 2: " + generator.generatePaths(map2));

        // 测试用例 3: 带减法的日期
        Map<Integer, String> map3 = new HashMap<>();
        map3.put(0, "20220922-1");
        map3.put(1, "20220111");
        System.out.println("Test 3: " + generator.generatePaths(map3));

        // 测试用例 4: 带减法和加法的组合
        Map<Integer, String> map4 = new HashMap<>();
        map4.put(0, "20220922-2");
        map4.put(1, "20220111+2");
        System.out.println("Test 4: " + generator.generatePaths(map4));
    }


}
